package psexecute.MailClasses;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;/**
 *
 * @author Giuseppe
 */
public class Mail {

    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final int SMTP_HOST_PORT = 465;
    private String SMTP_AUTH_USER;
    private String SMTP_AUTH_PWD;
    private String subject;
    private String txtmessage;

    public Mail(String subj, String msg)
    {
        this.subject=subj;
        this.txtmessage=msg;
    }

    public void sendTo(String dest, String usr, String pwd)
      throws MessagingException
  {
    Properties props = new Properties();

        props.put("mail.transport.protocol", "smtps");
        props.put("mail.smtps.host", SMTP_HOST_NAME);
        props.put("mail.smtps.auth", "true");
        props.put("mail.smtps.quitwait", "false");

        Session mailSession = Session.getDefaultInstance(props);
        mailSession.setDebug(true);
        Transport transport = mailSession.getTransport();

        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject(this.subject);
        message.setContent(this.txtmessage,"text/plain");
 
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(dest));

        transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, usr, pwd);

        transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
        transport.close();
  }

    public static void main (String[] args)
    {
        String oggetto = "Oggetto";
        String messaggio = "messaggio";
        String a = "destinatario";
        String user = "nome utente";
        String pwd = "password";
        Mail em = new Mail(oggetto,messaggio);
        try {
            em.sendTo(a, user, pwd);
        } catch (Exception ex) {
            System.out.println(ex.getStackTrace());
        }
    }

}
